const fs = require('fs');
const path = require('path');

const srcDir = path.join(__dirname, 'src');
const excludeDir = path.join(srcDir, 'static', 'locales');
const report = [];
const validExts = ['.ts', '.js', '.vue', '.json', '.scss', '.css', '.html'];

function isInLocales(filePath) {
  return filePath.startsWith(excludeDir);
}

function removeChinese(str) {
  // Match all Chinese characters
  return str.replace(/[\u4e00-\u9fa5]/g, '');
}

function processFile(filePath) {
  if (isInLocales(filePath)) return;
  const ext = path.extname(filePath);
  if (!validExts.includes(ext)) return;
  const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
  let changed = false;
  const newLines = lines.map((line, idx) => {
    const match = line.match(/[\u4e00-\u9fa5]+/g);
    if (match) {
      report.push({
        file: filePath.replace(__dirname + path.sep, ''),
        line: idx + 1,
        removed: match.join(''),
        original: line
      });
      changed = true;
      return removeChinese(line);
    }
    return line;
  });
  if (changed) {
    fs.writeFileSync(filePath, newLines.join('\n'), 'utf8');
  }
}

function walkDir(dir) {
  fs.readdirSync(dir).forEach(file => {
    const fullPath = path.join(dir, file);
    if (fs.statSync(fullPath).isDirectory()) {
      // exclude static/locales
      if (fullPath === excludeDir) return;
      walkDir(fullPath);
    } else {
      processFile(fullPath);
    }
  });
}

walkDir(srcDir);
fs.writeFileSync('report.json', JSON.stringify(report, null, 2), 'utf8');
console.log('Chinese characters have been deleted, please refer to the detailed changes for more information report.json'); 